package com.yulebaby.callsenter.task;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.yulebaby.callsenter.bean.PreSaleRemarkBean;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.ServerUrls;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.ui.activity.LoginActivity;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2016/12/8
 */
public class UploadPreSaleRemarkTask {

    /**
     * 添加售前跟踪记录  图片上传  录音文件上传 如果录音是网络地址只传录音的文件名
     *
     * @param bean
     */
    public static void doUploadPreSaleRemark(Activity activity, PreSaleRemarkBean bean, HttpCallback httpCallback) {
        if (bean != null) {
            HttpParams httpParams = new HttpParams();
            String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
            int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
            if (checkNull(user_token, activity)) {
                List<File> picList = new ArrayList<>();
                List<File> soundList = new ArrayList<>();
                List<String> fileNames = new ArrayList<>();
                httpParams.put("token", user_token);
                httpParams.put("sales_id", user_id);
                httpParams.put("customer_id", bean.getCustomer_id());
                httpParams.put("next_date", bean.getNext_date());
                httpParams.put("trace_content", bean.getRemarks());
                httpParams.put("source_type", bean.getVisit_type());
                if (bean.getTrack_pro() > 0) {
                    httpParams.put("track_pro", bean.getTrack_pro());
                }
                if (bean.getPicturefile() != null && bean.getPicturefile().size() > 0) {
                    for (String picPath : bean.getPicturefile()) {
                        if (picPath != null) {
                            picList.add(new File(picPath));
                        }
                    }
                    httpParams.putFileParams("picturefile", picList.size() > 0 ? picList : null);
                }

                if (bean.getSoundfile() != null && bean.getSoundfile().size() > 0) {
                    for (String soundPath : bean.getSoundfile()) {
                        if (soundPath != null) {
                            if (soundPath.startsWith("http")) {
                                String fileName = soundPath.substring(soundPath.lastIndexOf("/") + 1, soundPath.lastIndexOf("?"));
                                fileNames.add(fileName);
                            } else {
                                soundList.add(new File(soundPath));
                            }
                        }
                    }
                    httpParams.putFileParams("soundfile", soundList.size() > 0 ? soundList : null);
                    httpParams.putUrlParams("fileNames", fileNames.size() > 0 ? fileNames : null);
                }
                HttpHelper.getInstance().post(activity, ServerUrls.OPT_ADDPRESALESTRACE, httpParams, httpCallback);
            }
        }
    }

    /**
     * 判断是否为空
     *
     * @param string
     * @return
     */
    private static boolean checkNull(String string, Activity mActivity) {
        if (TextUtils.isEmpty(string)) {
            ToastUtil.showToast("登录信息出错,请重新登录");
            //TOKEN 失效跳转到登录界面
            SPUtil.remove(Constant.USER_TOKEN); //对登录信息进行清除
            SPUtil.remove(Constant.USER_ID);
            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
            mActivity.finish();
            return false;
        }
        return true;
    }
}
